package com.thankcreate.geekchat.logic;



import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.thankcreate.model.ContactEntry;

public abstract class React {
	private ContactEntry contactEntry;
	
	private String input;
	
	protected int lastOutputIndex;
	
	protected List<String> PossibleReacts;
	
	protected boolean isFirstNeedDifferent;
	
	protected boolean isEachNeedDifferent;

	protected int reactTime;
	
	public React(ContactEntry contactEntry,String input) {
		super();
		this.contactEntry = contactEntry;
		this.PossibleReacts = new ArrayList<String>();
		initPosiibleReacts();
		setLastOutputIndex(-1);
		// by default we don't want the first react different from others
		// and each output should be diffenet
		setFirstNeedDifferent(false);	
		// the react time set the time the computer cost to pretend to 
		// thinking before react
		setReactTime(1000);
		// this may not be used for this version
		setEachNeedDifferent(true);		
	}	
	
	public abstract String getReact();
	
	
	/*
	 * When overwrite this method, do not do any other initialization work 
	 * here. You should do them in the overwrited construction function
	 */
	public abstract void initPosiibleReacts();
	
	public String getInput() {
		return input;
	}

	public void setInput(String inputString) {
		this.input = inputString;
	}
	
	public List<String> getPossibleReacts() {
		return PossibleReacts;
	}

	public void setPossibleReacts(List<String> possibleReacts) {
		PossibleReacts = possibleReacts;
	}
	
	public ContactEntry getContactEntry() {
		return contactEntry;
	}

	public void setContactEntry(ContactEntry contactEntry) {
		this.contactEntry = contactEntry;
	}

	public int getLastOutputIndex() {
		return lastOutputIndex;
	}

	public void setLastOutputIndex(int lastOutput) {
		this.lastOutputIndex = lastOutput;
	}
	
	public boolean isFirstReact(){
		if(getLastOutputIndex() == -1){
			return true;
		}else {
			return false;
		}
	}
	
	public boolean isFirstNeedDifferent() {
		return isFirstNeedDifferent;
	}

	public void setFirstNeedDifferent(boolean isFirstNeedDifferent) {
		this.isFirstNeedDifferent = isFirstNeedDifferent;
	}
	
	
	public boolean isEachNeedDifferent() {
		return isEachNeedDifferent;
	}

	public void setEachNeedDifferent(boolean isEachNeedDifferent) {
		this.isEachNeedDifferent = isEachNeedDifferent;
	}

	
	public int getReactTime() {
		return reactTime;
	}

	public void setReactTime(int reactTime) {
		this.reactTime = reactTime;
	}

	/*
	 *  This function may not always return a different index from last time.
	 *  If the size of the possible react string is 1, we will just let it return 
	 *  the only one string.
	 *  This is helpful when we need to use Lanlanlu react since Lanlanlu can only say
	 *  "lanlanlu!".
	 */	
	public int getDifferetRandomIndex(){
		Random random = new Random();
		int randomInt;
		int total = getPossibleReacts().size();
		if(isFirstNeedDifferent()){
			if(isFirstReact())
			{
				randomInt = 0;			
			}
			else {
				while(((randomInt = random.nextInt(total)) == getLastOutputIndex())
						|| randomInt == 0) 
				{
				}			
			}
		}
		else {
			while(((randomInt = random.nextInt(total)) == getLastOutputIndex())) 
			{
				if(getPossibleReacts().size() == 1){
					break;
				}
			}
		}
		setLastOutputIndex(randomInt);
		return randomInt;
	}

}
